<?php
/**
 * Created by PhpStorm.
 * User: zmit
 * Date: 8/5/17
 * Time: 7:56 PM
 */
namespace ApiBundle\Controller\Platform;

use ApiBundle\Controller\BaseController;

use Symfony\Component\HttpFoundation\Request;

class SettingController extends BaseController
{
    /*
     * 完善个人信息
     */
    public function editSelfMessageAction(Request $request)
    {
        if(!$this->isPlatAdminLogged()){
            return $this->plat_response;
        }

        $name = $request->get("name");
        $phone = $request->get("phone");
        $email = $request->get("email");

        if(empty($name) || empty($phone) || empty($email)){
            return $this->jsonResponse(1,"参数错误");
        }

        $validations = $this->get("helper.validations");
        $strings = $this->get("helper.strings");

        if(!$validations->isMobile($phone)){
            return $this->jsonResponse(1,"手机号格式不正确");
        }

        if(!$validations->isEmail($email)){
            return $this->jsonResponse(1,"邮箱格式不正确");
        }

        $conn = $this->get("database_connection");
        $plat_admin = $conn->fetchAssoc("SELECT * FROM admin WHERE id = '". $this->getPlatId() ."'");

        $data = array(
            "realname" => $name,
            "email" => $email,
            "updated_at" =>date("Y-m-d H:i:s")
        );


        if($phone != $plat_admin['phone']){

            // 判断手机号是否存在
            if($this->checkPhoneExit($phone)){
                return $this->jsonResponse(1,"手机号已存在");
            }

            // 修改手机号，修改密码
            $data['phone'] = $phone;
            // $data['password'] = $strings->encryptPassword(substr($phone,-6,6),$plat_admin['salt']);
        }



        $where = array(
            "id" => $this->getPlatId()
        );
        $conn = $this->get("database_connection");

        $conn->update("admin",$data,$where);

        return $this->jsonResponse(0,"操作成功");

    }

    /*
     * 修改密码
     */
    public function updatePasswordAction(Request $request)
    {
        $conn = $this->get("database_connection");
        if(!$this->isPlatAdminLogged()){
            return $this->plat_response;
        }
        $password = $request->get("password");
        $new_password = $request->get("new_password");
        $comfirm_password = $request->get("comfirm_password");

        // TODO: 验证
        $validations = $this->get("helper.validations");
        $strings = $this->get("helper.strings");

        if(empty($password) || empty($new_password) || empty($comfirm_password)){
            return $this->jsonResponse(1,"参数错误");
        }

        if(!$validations->isPassword($new_password)){
            return $this->jsonResponse(1,"密码为6~18位");
        }
        if($new_password != $comfirm_password){
            return $this->jsonResponse(1,"密码与确认密码不一致");
        }

        // TODO： 查询
        $admin = $conn->fetchAssoc("SELECT * FROM admin WHERE id = '". $this->getPlatId() ."'");

        $password = $strings->encryptPassword($password,$admin['salt']);

        // TODO: 原密码判断
        if($admin['password'] != $password){
            return $this->jsonResponse(1,"原密码错误");
        }

        $encrypt_salt = $strings->generateSalt(6,true);
        $encrypt_password = $strings->encryptPassword($new_password,$encrypt_salt);

        $data = array(
            "password" => $encrypt_password,
            "salt" => $encrypt_salt
        );

        $conn->update("admin",$data,array("id" => $this->getPlatId() ));


        return $this->jsonResponse(0,"操作成功");
    }

    /*
     * 编辑超链接
     */
    public function linkManagerAction(Request $request)
    {
        $link = $request->get("link");
        $key_link = $request->get("key_link");

        if(empty($key_link)){
            return $this->jsonResponse(1,"文字不能为空");
        }

        $conn = $this->get("database_connection");

        $is_exit_link = $conn->fetchAssoc("SELECT * FROM settings WHERE type = 1");



        if($is_exit_link) {

            $conn->update("settings",array("config_key" => $key_link,"config_value"=>$link),array("type" => "1"));
        } else {
            $data = array(
                "type" => "1",
                "config_key" => $key_link,
                "config_value" => $link
            );
            $conn->insert("settings",$data);
        }

        return $this->jsonResponse(0,"操作成功");
    }

    /*
     * 获取超链接
     */

    public function getLinkAction()
    {
        $conn = $this->get("database_connection");

        $link = $conn->fetchAssoc("SELECT * FROM settings WHERE type = 1");
        return $this->jsonResponse(0,"操作成功",array("link"=>$link));
    }
}